package de.metalcon.gwt.server;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.neo4j.kernel.EmbeddedGraphDatabase;

public class ContextListener implements ServletContextListener {
	private static final String NEO_DB = "neo4j-instance";

	public static EmbeddedGraphDatabase getNeo4JInstance(
			ServletContext servletContext) {
		return (EmbeddedGraphDatabase) servletContext.getAttribute(NEO_DB);
	}

	public void contextInitialized(ServletContextEvent servletContextEvent) {
		EmbeddedGraphDatabase db;
		//TODO: attantion changed data base from /var/lib/neo4j/metalcon to /var/lib/neo4j/wiki
		db = new EmbeddedGraphDatabase("/var/lib/neo4j/wiki");

		ServletContext servletContext = servletContextEvent.getServletContext();
		servletContext.setAttribute(NEO_DB, db);
	}

	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		ServletContext servletContext = servletContextEvent.getServletContext();
		EmbeddedGraphDatabase db = getNeo4JInstance(servletContext);
		db.shutdown();
	}
}